package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheim.client.pojo.ShoppingCart;
import com.itheima.client.mapper.ShoppingCartMapper;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/19 19:35
 * @PackageName:com.itheima.service
 * @ClassName: ShoppingCartService
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车(菜品)
     * @param shoppingCart
     * @return
     */
    @Override
    public R addCommodityDish(ShoppingCart shoppingCart) {
        /*先查询是否有这条菜品和用户的记录*/
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", shoppingCart.getUserId());
        wrapper.eq("dish_id", shoppingCart.getDishId());
        List<ShoppingCart> dish = shoppingCartMapper.selectList(wrapper);
        /*有点话再添加直接更新数量*/
        if (dish.size()>0){
            shoppingCartMapper.addCount(shoppingCart);
            return R.success("添加菜品数量成功");
        }
        //添加新的菜品，数量为1
        shoppingCart.setNumber(1);
        int insert = shoppingCartMapper.insert(shoppingCart);
        if (insert>0){
            return R.success("添加菜品成功");
        }else {
            return R.error("添加异常");
        }
    }

    /**
     * 添加购物车（套餐）
     * @param shoppingCart
     * @return
     */
    @Override
    public R addCommoditySetmeal(ShoppingCart shoppingCart) {
        /*先查询是否有这条套餐和用户的记录*/
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", shoppingCart.getUserId());
        wrapper.eq("setmeal_id", shoppingCart.getSetmealId());
        List<ShoppingCart> setmeal = shoppingCartMapper.selectList(wrapper);
        /*有点话再添加直接更新数量*/
        if (setmeal.size()>0){
            shoppingCartMapper.addCount(shoppingCart);
            return R.success("添加套餐数量成功");
        }
        /*否则直接添加新的一条记录*/
        //添加新的菜品，数量为1
        shoppingCart.setNumber(1);
        int insert = shoppingCartMapper.insert(shoppingCart);
        if (insert>0){
            return R.success("添加套餐成功");
        }else {
            return R.error("添加套餐异常");
        }
    }

    /**
     * 购物车删除商品(菜品)
     * @param shoppingCart
     * @return
     */
    @Override
    public R dropCommodityDish(ShoppingCart shoppingCart) {
        /*先查询这条菜品和用户的记录的数量如何*/
        int number = shoppingCartMapper.selectNumber(shoppingCart);
        /*数量大于1时，更新数量*/
        if (number>1){
            shoppingCartMapper.dropCount(shoppingCart);
            return R.success("减少菜品数量成功");
        }
        /*否则删除这个记录*/
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", shoppingCart.getUserId());
        wrapper.eq("dish_id", shoppingCart.getDishId());
        int delete = shoppingCartMapper.delete(wrapper);
        if (delete>0){
            return R.success("删除菜品成功");
        }else {
            return R.error("删除菜品异常");
        }
    }

    /**
     * 购物车删除商品(套餐)
     * @param shoppingCart
     * @return
     */
    @Override
    public R dropCommoditySetmeal(ShoppingCart shoppingCart) {
        /*先查询这条菜品和用户的记录的数量如何*/
        int number = shoppingCartMapper.selectNumber(shoppingCart);
        /*数量大于1时，更新数量*/
        if (number>1){
            shoppingCartMapper.dropCount(shoppingCart);
            return R.success("减少套餐数量成功");
        }
        /*否则删除这个记录*/
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", shoppingCart.getUserId());
        wrapper.eq("setmeal_id", shoppingCart.getSetmealId());
        int delete = shoppingCartMapper.delete(wrapper);
        if (delete>0){
            return R.success("删除套餐成功");
        }else {
            return R.error("删除套餐异常");
        }
    }
}
